<?php 
	require_once("init.php");  
?>

<?php	
	$page = 0;
	if (isset($_GET["page"])) {
		$page = $_GET["page"];
	}
	$startRow = $page * $topicsPerPage;
	$endRow = ($page+1) * $topicsPerPage;
	
	// Topics Count
  $qMyOpenTopicsCount = mysql_query("
    SELECT 
			count(topicId) AS totalCount
    FROM topics
    WHERE statusId = ".$statusActive."
			AND now() < dateClosed
			AND userId = ".$_SESSION["userId"]."
	");

  // Topics
  $qMyOpenTopics = mysql_query("
    SELECT 
      t.*,
      c.name AS categoryName
    FROM topics t
      INNER JOIN categories c ON t.categoryId = c.categoryId
    WHERE statusId = ".$statusActive."
			AND now() < dateClosed
			AND userId = ".$_SESSION["userId"]."
		ORDER BY dateClosed DESC
    LIMIT ".$startRow.", ".$endRow.";
  ");
	
	$showMore = true;
	$totalCount = mysql_fetch_array($qMyOpenTopicsCount);
	$totalCount = $totalCount["totalCount"];
	if ($endRow >= $totalCount) $showMore = false;
	if ($totalCount == 0) {
?>
<li>No topics found</li>
<?php
	} else {
		while ($row = mysql_fetch_array($qMyOpenTopics)) {
?>
<li>
	<a href="viewmytopic.php?id=<?php print $row["topicId"]; ?>">
		<?php print $row["question"]; ?><br />
		<small><strong><?php print $row["answer1"]." or ".$row["answer2"]; ?>?</strong></small>
	</a>
</li>
<?php
		}
	}
?>

<?php
	if ($showMore) {
?>
<li class="more">
	<a href="listmyopentopic.php?page=<?php print ($page+1) ?>" target="_replace"><?php print $topicsPerPage; ?> more topics...</a>
</li>
<?php
	}
?>